const Base = require('./base.js');
const { think } = require('thinkjs');

module.exports = class extends Base {
  // async textAction() {
  //   const list = await this.model('output_list')
  //     .where({ contract_id: 4859 })
  //     .field('remarkCircle, id')
  //     .select();
  //   for (const item of list) {
  //     if (item.remarkCircle.includes('物业')) {
  //       console.log(item.remarkCircle);
  //       // await this.model('output_list')
  //       //   .where({ id: item.id })
  //       //   .update({
  //       //     type: 'wuye',
  //       //   });
  //     }
  //   }
  //   return this.success(list);
  // }
  async listAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;

    const model = this.model('user_list');
    const query = this.get();

    delete query.page;
    delete query.size;

    if (query.phone) {
      query.phone = ['like', `%${query.phone}%`];
    }

    if (query.name) {
      query.name = ['like', `%${query.name}%`];
    }
    query.status = 0;
    query.phone = ['!=', null];

    const data = await model
      .where(query)
      .order(['id DESC'])
      .page(page, size)
      .countSelect();

    return this.success(data);
  }

  async listAllAction() {
    const data = await this.model('user_list')
      .where({ status: 0 })
      .select();
    return this.success(data);
  }

  async talentsAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;

    const query = this.get();

    delete query.page;
    delete query.size;

    if (query.phone) {
      query.phone = ['like', `%${query.phone}%`];
    }

    if (query.name) {
      query.name = ['like', `%${query.name}%`];
    }

    query['a.status'] = 0;
    query['a.phone'] = ['!=', null];
    query['p.interview_status'] = 'INTERVIEW_FINISH';

    // const countData = await this.model('process')
    //   .where({ status: 3 })
    //   .distinct('user_id')
    //   .select();

    const data = await this.model('process')
      .alias('p')
      .join({
        user_list: {
          join: 'left',
          as: 'a',
          on: ['user_id', 'id'],
        },
        post_list: {
          join: 'left',
          as: 'j',
          on: ['job_id', 'id'],
        },
      })
      .field(
        'a.id as user_id, a.name, a.phone, a.form, a.tag, a.gender, p.interview_time, p.score, j.name as job_name, p.orderToken'
      )
      .where(query)
      .order(['p.id DESC'])
      .page(page, size)
      .countSelect();
    // for (const item of data.data) {
    //   item.interview_time = await this.model('process')
    //     .where({ user_id: item.id })
    //     .order(['id DESC'])
    //     .getField('interview_time', true);
    // }

    return this.success(data);
  }

  async detailAction() {
    const id = this.get('id');
    const data = await this.model('user_list')
      .where({ id: id })
      .find();

    return this.success(data);
  }

  async updateAction() {
    const id = this.post('id');
    const status = this.post('working');
    const model = this.model('user_list');
    await model.where({ id: id }).update({
      working: status,
    });

    return this.success();
  }

  async deleteAction() {
    const id = this.get('id');
    await this.model('user_list')
      .where({ id: id })
      .limit(1)
      .delete();

    return this.success();
  }
};
